############################################################################
##
## Copyright (C) 2006-2007 University of Utah. All rights reserved.
##
## This file is part of VisTrails.
##
## This file may be used under the terms of the GNU General Public
## License version 2.0 as published by the Free Software Foundation
## and appearing in the file LICENSE.GPL included in the packaging of
## this file.  Please review the following to ensure GNU General Public
## Licensing requirements will be met:
## http://www.opensource.org/licenses/gpl-license.php
##
## If you are unsure which license is appropriate for your use (for
## instance, you are interested in developing a commercial derivative
## of VisTrails), please contact us at vistrails@sci.utah.edu.
##
## This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
## WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
##
############################################################################
"""
Contain color related utilities such as color look up by name or
color defines
"""

################################################################################

class ColorByName(object):
    """
    Provides a list of colors that can be queried by name
    
    """
    colors = { 'antique_white' : [ 0.9804, 0.9216, 0.8431 ],
               'azure' : [ 0.9412, 1.0000, 1.0000 ],
               'bisque' : [ 1.0000, 0.8941, 0.7686 ],
               'blanched_almond' : [ 1.0000, 0.9216, 0.8039 ],
               'cornsilk' : [ 1.0000, 0.9725, 0.8627 ],
               'eggshell' : [ 0.9900, 0.9000, 0.7900 ],
               'floral_white' : [ 1.0000, 0.9804, 0.9412 ],
               'gainsboro' : [ 0.8627, 0.8627, 0.8627],
               'ghost_white' : [ 0.9725, 0.9725, 1.0000],
               'honeydew' : [ 0.9412, 1.0000, 0.9412 ],
               'ivory' : [ 1.0000, 1.0000, 0.9412 ],
               'lavender' : [ 0.9020, 0.9020, 0.9804],
               'lavender_blush' : [1.0000, 0.9412, 0.9608 ],
               'lemon_chiffon' : [1.0000, 0.9804, 0.8039 ],
               'linen' : [ 0.9804, 0.9412, 0.9020 ],
               'mint_cream' : [ 0.9608, 1.0000, 0.9804 ],
               'misty_rose' : [ 1.0000, 0.8941, 0.8824 ],
               'moccasin' : [ 1.0000, 0.8941, 0.7098 ],
               'navajo_white' : [ 1.0000, 0.8706, 0.6784 ], 
               'old_lace' : [ 0.9922, 0.9608, 0.9020 ], 
               'papaya_whip' : [ 1.0000, 0.9373, 0.8353 ], 
               'peach_puff' : [ 1.0000, 0.8549, 0.7255 ], 
               'seashell' : [ 1.0000, 0.9608, 0.9333 ], 
               'snow' : [ 1.0000, 0.9804, 0.9804 ],
               'thistle' : [ 0.8471, 0.7490, 0.8471 ], 
               'titanium_white' : [ 0.9900, 1.0000, 0.9400 ],
               'wheat' : [ 0.9608, 0.8706, 0.7020 ],
               'white' : [ 1.0000, 1.0000, 1.0000 ],
               'white_smoke' : [ 0.9608, 0.9608, 0.9608 ],
               'zinc_white' : [ 0.9900, 0.9700, 1.0000 ],

               #Greys
               'cold_grey' : [ 0.5000, 0.5400, 0.5300 ], 
               'light_dim_grey' : [ 0.45, 0.45, 0.45 ],
               'dim_grey' : [ 0.4118, 0.4118, 0.4118 ],
               'dark_dim_grey' : [ 0.35, 0.35, 0.35 ],
               'grey' : [ 0.7529, 0.7529, 0.7529 ],
               'very_light_grey' : [ 0.95, 0.95, 0.95 ],
               'light_grey' : [ 0.8275, 0.8275, 0.8275 ],
               'slate_grey' : [ 0.4392, 0.5020, 0.5647 ],
               'slate_grey_dark' : [ 0.1843, 0.3098, 0.3098 ],
               'slate_grey_light' : [ 0.4667, 0.5333, 0.6000 ],
               'warm_grey' : [ 0.5000, 0.5000, 0.4100 ],

               #Blacks
               'black' : [ 0.0000, 0.0000, 0.0000], 
               'ivory_black' : [ 0.1600, 0.1400, 0.1300 ],
               'lamp_black' : [ 0.1800, 0.2800, 0.2300 ],

               #Reds
               'alizarin_crimson' : [ 0.8900, 0.1500, 0.2100 ], 
               'brick' : [ 0.6100, 0.4000, 0.1200 ],
               'cadmium_red_deep' : [ 0.8900, 0.0900, 0.0500 ],
               'coral' : [ 1.0000, 0.4980, 0.3137 ],
               'coral_light' : [ 0.9412, 0.5020, 0.5020 ],
               'deep_pink' : [ 1.0000, 0.0784, 0.5765], 
               'english_red' : [ 0.8300, 0.2400, 0.1000 ], 
               'firebrick' : [ 0.6980, 0.1333, 0.1333 ],
               'geranium_lake' : [ 0.8900, 0.0700, 0.1900 ],
               'hot_pink' : [ 1.0000, 0.4118, 0.7059 ], 
               'indian_red' : [ 0.6900, 0.0900, 0.1200 ],
               'light_salmon' : [ 1.0000, 0.6275, 0.4784 ],
               'madder_lake_deep' :[ 0.8900, 0.1800, 0.1900 ],
               'maroon' : [0.6902, 0.1882, 0.3765 ],
               'pink' : [ 1.0000, 0.7529, 0.7961 ],
               'pink_light' : [ 1.0000, 0.7137, 0.7569 ],
               'raspberry' : [ 0.5300, 0.1500, 0.3400 ],
               'red': [ 1.0000, 0.0000, 0.0000 ],
               'rose_madder' : [ 0.8900, 0.2100, 0.2200 ],
               'salmon' : [ 0.9804, 0.5020, 0.4471 ],
               'tomato': [ 1.0000, 0.3882, 0.2784 ],
               'venetian_red' : [ 0.8300, 0.1000, 0.1200 ],

               #Browns
               'beige' : [ 0.6400, 0.5800, 0.5000 ],  
               'brown' : [ 0.5000, 0.1647, 0.1647 ],  
               'brown_madder' : [ 0.8600, 0.1600, 0.1600 ],  
               'brown_ochre': [ 0.5300, 0.2600, 0.1200 ],  
               'burlywood' : [ 0.8706, 0.7216, 0.5294 ],  
               'burnt_sienna' : [ 0.5400, 0.2100, 0.0600 ],  
               'burnt_umber' : [ 0.5400, 0.2000, 0.1400 ],  
               'chocolate' : [ 0.8235, 0.4118, 0.1176 ],  
               'deep_ochre' : [ 0.4500, 0.2400, 0.1000 ],  
               'flesh': [ 1.0000, 0.4900, 0.2500 ],  
               'flesh_ochre': [ 1.0000, 0.3400, 0.1300 ],  
               'gold_ochre': [ 0.7800, 0.4700, 0.1500 ],  
               'greenish_umber': [ 1.0000, 0.2400, 0.0500 ],  
               'khaki': [ 0.9412, 0.9020, 0.5490 ],  
               'khaki_dark': [ 0.7412, 0.7176, 0.4196 ],  
               'light_beige': [ 0.9608, 0.9608, 0.8627 ],  
               'peru': [ 0.8039, 0.5216, 0.2471 ],  
               'rosy_brown': [ 0.7373, 0.5608, 0.5608 ],  
               'raw_sienna': [ 0.7800, 0.3800, 0.0800 ],  
               'raw_umber': [ 0.4500, 0.2900, 0.0700 ],  
               'sepia': [ 0.3700, 0.1500, 0.0700 ],  
               'sienna': [ 0.6275, 0.3216, 0.1765 ],  
               'saddle_brown': [ 0.5451, 0.2706, 0.0745 ],  
               'sandy_brown': [ 0.9569, 0.6431, 0.3765 ],  
               'tan': [ 0.8235, 0.7059, 0.5490 ],  
               'van_dyke_brown': [ 0.3700, 0.1500, 0.0200 ],  

               #Oranges
               'cadmium_orange': [ 1.0000, 0.3800, 0.0100 ],  
               'cadmium_red_light': [ 1.0000, 0.0100, 0.0500 ],  
               'carrot': [ 0.9300, 0.5700, 0.1300 ],  
               'dark_orange': [ 1.0000, 0.5490, 0.0000 ],  
               'mars_orange': [ 0.5900, 0.2700, 0.0800 ],  
               'mars_yellow': [ 0.8900, 0.4400, 0.1000 ],  
               'orange': [ 1.0000, 0.5000, 0.0000 ],  
               'orange_red': [ 1.0000, 0.2706, 0.0000 ],  
               'yellow_ochre': [ 0.8900, 0.5100, 0.0900 ],  
               
               #Yellows
               'aureoline_yellow': [ 1.0000, 0.6600, 0.1400 ],  
               'banana': [ 0.8900, 0.8100, 0.3400 ],  
               'cadmium_lemon': [ 1.0000, 0.8900, 0.0100 ],  
               'cadmium_yellow': [ 1.0000, 0.6000, 0.0700 ],  
               'cadmium_yellow_light': [ 1.0000, 0.6900, 0.0600 ],  
               'gold': [ 1.0000, 0.8431, 0.0000 ],  
               'goldenrod': [ 0.8549, 0.6471, 0.1255 ],  
               'goldenrod_medium': [0.8549, 0.6971, 0.2255],
               'goldenrod_dark': [ 0.7216, 0.5255, 0.0431 ],  
               'goldenrod_light': [ 0.9804, 0.9804, 0.8235 ],  
               'goldenrod_pale': [ 0.9333, 0.9098, 0.6667 ],  
               'light_goldenrod': [ 0.9333, 0.8667, 0.5098 ],  
               'melon': [ 0.8900, 0.6600, 0.4100 ],  
               'naples_yellow_deep': [ 1.0000, 0.6600, 0.0700 ],  
               'yellow': [ 1.0000, 1.0000, 0.0000 ],  
               'yellow_light': [ 1.0000, 1.0000, 0.8784 ],  

               #Greens
               'chartreuse': [ 0.4980, 1.0000, 0.0000 ],  
               'chrome_oxide_green': [ 0.4000, 0.5000, 0.0800 ],  
               'cinnabar_green': [ 0.3800, 0.7000, 0.1600 ],  
               'cobalt_green': [ 0.2400, 0.5700, 0.2500 ],  
               'emerald_green': [ 0.0000, 0.7900, 0.3400 ],  
               'forest_green': [ 0.1333, 0.5451, 0.1333 ],  
               'green': [ 0.0000, 1.0000, 0.0000 ],  
               'green_dark': [ 0.0000, 0.3922, 0.0000 ],  
               'green_pale': [ 0.5961, 0.9843, 0.5961 ],  
               'green_yellow': [ 0.6784, 1.0000, 0.1843 ],  
               'lawn_green': [ 0.4863, 0.9882, 0.0000 ],  
               'lime_green': [ 0.1961, 0.8039, 0.1961 ],  
               'mint': [ 0.7400, 0.9900, 0.7900 ],  
               'olive': [ 0.2300, 0.3700, 0.1700 ],  
               'olive_drab': [ 0.4196, 0.5569, 0.1373 ],  
               'olive_green_dark': [ 0.3333, 0.4196, 0.1843 ],  
               'permanent_green': [ 0.0400, 0.7900, 0.1700 ],  
               'sap_green': [ 0.1900, 0.5000, 0.0800 ],  
               'sea_green': [ 0.1804, 0.5451, 0.3412 ],  
               'sea_green_dark': [ 0.5608, 0.7373, 0.5608 ],  
               'sea_green_medium': [ 0.2353, 0.7020, 0.4431 ],  
               'sea_green_light': [ 0.1255, 0.6980, 0.6667 ],  
               'spring_green': [ 0.0000, 1.0000, 0.4980 ],  
               'spring_green_medium': [ 0.0000, 0.9804, 0.6039 ],  
               'terre_verte': [ 0.2200, 0.3700, 0.0600 ],  
               'viridian_light': [ 0.4300, 1.0000, 0.4400 ],  
               'yellow_green': [ 0.6039, 0.8039, 0.1961 ],  

               #Cyans
               'aquamarine': [ 0.4980, 1.0000, 0.8314 ],  
               'aquamarine_medium': [ 0.4000, 0.8039, 0.6667 ],  
               'cyan': [ 0.0000, 1.0000, 1.0000 ],  
               'cyan_white': [ 0.8784, 1.0000, 1.0000 ],  
               'turquoise': [ 0.2510, 0.8784, 0.8157 ],  
               'turquoise_dark': [ 0.0000, 0.8078, 0.8196 ],  
               'turquoise_medium': [ 0.2824, 0.8196, 0.8000 ],  
               'turquoise_pale': [ 0.6863, 0.9333, 0.9333 ],  
               
               #Blues
               'alice_blue': [ 0.9412, 0.9725, 1.0000 ],  
               'blue': [ 0.0000, 0.0000, 1.0000 ],  
               'blue_light': [ 0.6784, 0.8471, 0.9020 ],  
               'blue_medium': [ 0.0000, 0.0000, 0.8039 ],  
               'cadet': [ 0.3725, 0.6196, 0.6275 ],  
               'cobalt': [ 0.2400, 0.3500, 0.6700 ],  
               'cornflower': [ 0.3922, 0.5843, 0.9294 ],  
               'desatcornflower': [ 0.5138, 0.6098, 0.7835 ],  
               'cerulean': [ 0.0200, 0.7200, 0.8000 ],  
               'dodger_blue': [ 0.1176, 0.5647, 1.0000 ],  
               'indigo': [ 0.0300, 0.1800, 0.3300 ],  
               'manganese_blue': [ 0.0100, 0.6600, 0.6200 ],  
               'midnight_blue': [ 0.0980, 0.0980, 0.4392 ],  
               'navy': [ 0.0000, 0.0000, 0.5020 ],  
               'peacock': [ 0.2000, 0.6300, 0.7900 ],  
               'powder_blue': [ 0.6902, 0.8784, 0.9020 ],  
               'royal_blue': [ 0.2549, 0.4118, 0.8824 ],  
               'slate_blue': [ 0.4157, 0.3529, 0.8039 ],  
               'slate_blue_dark': [ 0.2824, 0.2392, 0.5451 ],  
               'slate_blue_light': [ 0.5176, 0.4392, 1.0000 ],  
               'slate_blue_medium': [ 0.4824, 0.4078, 0.9333 ],  
               'sky_blue': [ 0.5294, 0.8078, 0.9216 ],  
               'sky_blue_deep': [ 0.0000, 0.7490, 1.0000 ],  
               'sky_blue_light': [ 0.5294, 0.8078, 0.9804 ],  
               'steel_blue': [ 0.2745, 0.5098, 0.7059 ],  
               'steel_blue_light': [ 0.6902, 0.7686, 0.8706 ],  
               'turquoise_blue': [ 0.0000, 0.7800, 0.5500 ],  
               'ultramarine': [ 0.0700, 0.0400, 0.5600 ],  
               
               #Magentas
               'blue_violet': [ 0.5412, 0.1686, 0.8863 ],  
               'cobalt_violet_deep': [ 0.5700, 0.1300, 0.6200 ],  
               'magenta': [ 1.0000, 0.0000, 1.0000 ],  
               'orchid': [ 0.8549, 0.4392, 0.8392 ],  
               'orchid_dark': [ 0.6000, 0.1961, 0.8000 ],  
               'orchid_medium': [ 0.7294, 0.3333, 0.8275 ],  
               'permanent_red_violet': [ 0.8600, 0.1500, 0.2700 ],  
               'plum': [ 0.8667, 0.6275, 0.8667 ],  
               'purple': [ 0.6275, 0.1255, 0.9412 ],  
               'purple_medium': [ 0.5765, 0.4392, 0.8588 ],  
               'ultramarine_violet': [ 0.3600, 0.1400, 0.4300 ],  
               'violet': [ 0.5600, 0.3700, 0.6000 ],  
               'violet_dark': [ 0.5804, 0.0000, 0.8275 ],  
               'violet_red': [ 0.8157, 0.1255, 0.5647 ],  
               'violet_red_medium': [ 0.7804, 0.0824, 0.5216 ],  
               'violet_red_pale': [ 0.8588, 0.4392, 0.5765 ] }
    @staticmethod
    def get(name):
        """ get(str) -> Color        
        This is a static method returning a color with the
        given name or black if name not found. Alpha channel is also
        included
        
        """
        if name in ColorByName.colors:
            return ColorByName.colors[name] + [1.0]
        else:
            return [0.0, 0.0, 0.0, 1.0]

    @staticmethod
    def get_int(name):
        """ get_int(str) -> Color in int range
        Similar to get() but color ranges are from 0 to 255
        
        """
        if name in ColorByName.colors:
            return [int(ColorByName.colors[name][0]*255),
                    int(ColorByName.colors[name][1]*255),
                    int(ColorByName.colors[name][2]*255),
                    255]
        else:
            return [0, 0, 0, 255]

    @staticmethod
    def get_no_alpha(name):
        """ get_no_alpha(str) -> Color        
        This is a static method returning a color with the given name
        or black if name not found. Alpha channel is not included
        
        """
        if name in ColorByName.colors:
            return ColorByName.colors[name]+[] # to make copy
        else:
            return [0.0, 0.0, 0.0]

    @staticmethod
    def get_no_alpha_int(name):
        """ get_no_alpha_int(str) -> Color in int range
        Similar to get_no_alpha() but color ranges are from 0 to 255
        
        """
        if name in ColorByName.colors:
            return [int(ColorByName.colors[name][0]*255),
                    int(ColorByName.colors[name][1]*255),
                    int(ColorByName.colors[name][2]*255)]
        else:
            return [0, 0, 0]

class ColorManipulator(object):
    """
    ColorManipulator is a class containing color-manipulation routines
    
    """
    @staticmethod
    def multiply(color, ratio):
        """ multiply(color: list, ratio: float) -> Color        
        This is a static method returning a color that has its RGB to
        be only a ratio of color
        
        """
        result = []
        for i in xrange(len(color)):
            if i<3:
                result.append(color[i]*ratio)
            else:
                return result + color[3:]
        return color

################################################################################

import unittest

class TestColorByName(unittest.TestCase):
    """
    A few simple tests to make sure ColorByName is working as expected
    
    """
    def test_alpha_channel(self):
        self.assertEquals(ColorByName.get('blue'),
                          [0.0, 0.0, 1.0 , 1.0])
        self.assertEquals(ColorByName.get_no_alpha('red'),
                          [1.0, 0.0, 0.0])
        self.assertEquals(ColorByName.get('not exist'),
                          [0.0, 0.0, 0.0, 1.0]),        
        self.assertEquals(ColorByName.get_no_alpha('another not exist'),
                          [0.0, 0.0, 0.0]),
                
if __name__ == '__main__':
    unittest.main()
